package ru.spb.osll.myfit.fixtures.log;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class MyFitLogger {
  private static LinkedList<FileAppender> appendersToClose = new LinkedList<FileAppender>();
  public static Logger getLogger(Class< ? > clazz) {
    Logger logger = Logger.getLogger(MyFitLogger.class);

    ClassLoader classLoader = clazz.getClassLoader();
    logger.debug("class loader " + classLoader);

    URL[] ur =((URLClassLoader) classLoader).getURLs();
    for (URL url : ur) {
    	logger.debug("class loader url " + url.getFile());
	}
    try {
      URL urlLog = classLoader.getResource("logconfig.properties");	
      if(urlLog != null){
    		logger.debug("class loader url " + urlLog.getFile());	  
      }else{
    		logger.debug("class loader url NULL (((((");
      }
      ResourceBundle bundle = ResourceBundle.getBundle("logconfig",
          Locale.getDefault(), classLoader);
      String filename = bundle.getString("logFile");
      String loggerName = bundle.getString("loggerName");

      logger.debug("filename " + filename);
      Logger LOG = createLoger(loggerName, filename);
      return LOG;

    } catch (Exception e) {

      logger.warn("Error", e);
      // TODO: resolve this problem, It's not understand for me why path lost"../../"
         
      //may be path "../../" + path resourse  which you wonna find (((, will find;
      FileInputStream in = null;
      try {
    	URL url = classLoader.getResource("logconfig.properties");

        logger.debug("logfile path " + url.getFile());

        in = new FileInputStream(".." + File.separator + ".." + File.separator + url.getFile());
        PropertyResourceBundle bundle = new PropertyResourceBundle(in);

        String filename = bundle.getString("logFile");
        String loggerName = bundle.getString("loggerName");
        in.close();
        logger.debug("filename " + filename);
        Logger LOG = createLoger(loggerName, filename);
        return LOG;

      } catch (Exception ex) {
        logger.debug("can not find bundle", ex);
      }finally{
    	  if(in != null){
    		  try {
				in.close();
			} catch (IOException e1) {
		
			}
    	  }
      }
    }
    return Logger.getLogger(clazz);
  }

  @SuppressWarnings("rawtypes")
  private static Logger createLoger(String loggerName, String logFile) {
    Logger LOG = Logger.getLogger(loggerName);
    Enumeration allAppenders = LOG.getAllAppenders();
    if(!allAppenders.hasMoreElements()){
      FileAppender appender;
       try {
    	
         appender = new FileAppender(new PatternLayout(
              "%d{ABSOLUTE} %5p  %C{1} - %m%n"), logFile);
         LOG.addAppender(appender);
         appendersToClose.add(appender);
       } catch (Exception e) {
       }
      LOG.setAdditivity(false);
      LOG.setLevel(Level.INFO);
    }
    return LOG;
  }
  
  public static void closeAppenders(){
	  for(FileAppender appender: appendersToClose){
		  appender.close();
	  }
  }
}
